package com.feilong.cn.mybatis.Auto;

import java.sql.*;

/**
 * Created by yangxulong on 2018/4/2.
 */
public class AutoExecutor implements IExecutor {
    @Override
    public <T> T query(String strStateMent) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        UserBean test = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://10.168.100.164:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC", "root", "root");

            preparedStatement = connection.prepareStatement(strStateMent);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                test = new UserBean();
                test.setId(rs.getInt(1));
                test.setName(rs.getString(2));
                test.setAge(rs.getInt(3));
                test.setSex(rs.getBoolean(4));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return (T)test;
    }
}
